﻿CREATE PROCEDURE [dbo].[spCreateNewCompany]
	( @shortname nvarchar(50), @longname nvarchar(100) )
as
begin
	set nocount on
	declare @OwnerID int
	begin transaction

	insert into tblOwner ( SHortName, LongName, AllowPartialShipments ) values ( @shortname, @longname, 0 )
	set @OwnerID = SCOPE_IDENTITY()

	-- Set up the default owner accounts
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Cash', 'A', @OwnerID, 1 )
	update tblOwner set CashAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Product Revenue', 'R', @OwnerID, 1 )
	update tblOwner set ProductRevenueAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Inventory', 'A', @OwnerID, 1 )
	update tblOwner set InventoryAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Cost of Goods Sold', 'E', @OwnerID, 1 )
	update tblOwner set CostOfGoodsSoldAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Cost of Goods Adjustments', 'E', @OwnerID, 1 )
	update tblOwner set CostOfGoodsAdjustmentAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Shipping Cost', 'E', @OwnerID, 1 )
	update tblOwner set ShippingCostAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Shipping Revenue', 'R', @OwnerID, 1 )
	update tblOwner set ShippingRevenueAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Tax Revenue', 'R', @OwnerID, 1 )
	update tblOwner set TaxRevenueAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID
	insert into tblAccount ( CreateDate, Name, AccountTypeCode, OwnerID, CurrencyID )
	values ( getdate(), 'Misc Expenses', 'E', @OwnerID, 1 )
	update tblOwner set MiscExpenseAccountID = SCOPE_IDENTITY() where OwnerID = @OwnerID

	insert into codeProductPricingClass ( OwnerID, Description )
	values ( @OwnerID, 'Default' )

	commit
	return @OwnerID
end
